package database.entity;

import database.DatabaseWorker;
import java.util.Date;
import org.apache.commons.codec.binary.Base64;
import security.AESKey;
import security.RSAKeyPair;

/**
 *
 * @author Tom
 */
public class User 
{
    public int UserID;
    public String Username;
    public String Email;
    public String Password;
    public byte[] PublicKey;
    public byte[] PrivateKey;        
    public String IncomingServer;
    public String Protocol;
    public String OutgoingServer;
    public String Port;
    public boolean SSL;
    public String EmailPassword;
    public Date LastSync;
    public int SyncPeriod;
    public String AESEncryptKey;
        
    public DatabaseWorker databaseWorker;
    public MailBox[] mailboxes;
    public ContactGroup[] contactgroup;
    
    public RSAKeyPair keypair;    
    public AESKey aesKey;
        
    public User(int userID, String username, String email, String password, 
            String publickey, String privatekey,
            String incomingServer, String protocol, String outgoingServer, String port, boolean ssl, 
            String emailPassword, Date lastSync, int syncPeriod, String AESEncryptKey, AESKey aes)
    {
        Base64 base64 = new Base64();
        this.UserID=userID;
        this.Username=username;
        this.Email=email;
        this.Password=password;
        this.PublicKey=base64.decode(publickey);
        try
        {
            this.PrivateKey=aes.decode(base64.decode(privatekey));
        }
        catch (Exception ex)
        {
            this.PrivateKey=null;
        }
        this.IncomingServer=incomingServer;
        this.OutgoingServer=outgoingServer;
        this.Port=port;
        this.Protocol=protocol;
        this.SSL=ssl;
        try
        {
            this.EmailPassword=new String(aes.decode(base64.decode(emailPassword)));
        }
        catch (Exception ex)
        {
            this.EmailPassword=null;
        }
        this.LastSync=lastSync;
        this.SyncPeriod=syncPeriod;
        this.AESEncryptKey=AESEncryptKey;
        
        this.aesKey = aes;
        keypair = new RSAKeyPair(this.PublicKey, this.PrivateKey);        
        databaseWorker = new DatabaseWorker(this);
    }
}
